package com.xiangying.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiangying.entity.common.Result;
import com.xiangying.utils.ResultUtils;

/**
 * 异常处理
 * @author luolihong
 *
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @SuppressWarnings("rawtypes")
	@ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        if (e instanceof BusinessException) {
        	logger.error("系统异常:{}", e.getMessage());
            BusinessException exception = (BusinessException) e;
            return ResultUtils.error(exception.getCode(), exception.getMessage());
        }else {
        	logger.error("系统异常", e);
            return ResultUtils.error(-1, "未知错误");
        }
    }
}
